import { encapsulation,request } from '@/network/request.js'
import {getToken} from '@/network/bookManager'

/*
 *@description: 查询所有 书籍
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-22 22:57:30
 */
export function queryBookAll(option) {
  return encapsulation({
    url: `/api/tcb/databasequery`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
            .where({
              ${option.authorId?`authorId:'${option.authorId}',`:``}
                bookStatus:${option.bookStatus}
            }).limit(1000)
            .get()
          `
    }
  })
}
/*
 *@description: 添加章节
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-23 01:33:53
 */
export function addChapter(option) {
  return encapsulation({
    url: `/api/tcb/databaseadd`,
    method: 'post',
    data: {
      query: `db.collection('boxChapterContent')
            .add({
                data:[{
                  ${option.chapterBookId?`chapterBookId: '${option.chapterBookId}',`:``}
                  ${option.chapterLaunchTime?`chapterLaunchTime: new Date('${option.chapterLaunchTime}'),`:``}
                  ${option.chapterName?`chapterName: '${option.chapterName}',`:``}
                  ${option.chapterNumber?`chapterNumber: ${option.chapterNumber},`:``}
                  ${option.chapterContentUrl?`chapterContentUrl: '${option.chapterContentUrl}',`:``}
                  ${option.chapterStatus!=undefined?`chapterStatus:${option.chapterStatus}`:``}
                }]
               
            })
          `
    }
  })
}
/*
 *@functionName: queryChapterLately
 *@params1: 参数1
 *@description: 最近 章节记录
 *@author: chendi
 *@date: 2020-08-23 13:57:06
 *@version: V1.0.5
 */
export function queryChapterLately(chapterBookId, limit,sort,status) {
  return encapsulation({
    url: `/api/tcb/databasequery`,
    method: 'post',
    data: {
      query: `db.collection('boxChapterContent')
      .where({
         chapterBookId:'${chapterBookId}',
          ${status!=undefined?`chapterStatus:${status}`:``}
      })
      .orderBy('chapterLaunchTime','${sort||'asc'}')
      .limit(${limit || 1000})
      .get()`
    }
  })
}
/*
 *@description: 统计当前文章有多少个
 *@author: chendi
 *@date: 2020-08-23 20:16:52
 *@version: V1.0.5
 */
export function queryChapterCountByBookId(chapterBookId) {
  return encapsulation({
    url: `/api/tcb/databasecount`,
    method: 'post',
    data: {
      query: `db.collection('boxChapterContent')
      .where({
         chapterBookId:'${chapterBookId}',
              chapterStatus:1
      })
      .count()`
    }
  })
}
/*
 *@functionName:  根据id修改章节
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-08-24 10:55:34
 *@version: V1.0.5
*/
export function modifyChapterByBookId(option) {
  return encapsulation({
    url: `/api/tcb/databaseupdate`,
    method: 'post',
    data: {
      query: `db.collection('boxChapterContent')
      .where({
         _id:'${option._id}'
      }).update({
        data:{
          ${option.chapterBookId?`chapterBookId: '${option.chapterBookId}',`:``}
          ${option.chapterLaunchTime?`chapterLaunchTime: new Date('${option.chapterLaunchTime}'),`:``}
          ${option.chapterName?`chapterName: '${option.chapterName}',`:``}
          ${option.chapterNumber?`chapterNumber: ${option.chapterNumber},`:``}
          ${option.chapterContentUrl?`chapterContentUrl: '${option.chapterContentUrl}',`:``}
          ${option.chapterStatus!=undefined?`chapterStatus:${option.chapterStatus}`:``}
        }
      })`
    }
  })
}
/*
 *@functionName:  查询
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-01 16:47:23
 *@version: V1.0.5
*/
export async function chapterDustbin(option) {
  let token = await getToken()
  return request({
    url: `/api/tcb/invokecloudfunction?access_token=${token.access_token}&env=zhuma-mbnti&name=chapterdustbin`,
    method: 'post',
    data: JSON.stringify(option)
  })
}
/*
 *@functionName:  真实删除章节
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-01 19:22:28
 *@version: V1.0.5
*/
export function removeChapterByBookId(option) {
  return encapsulation({
    url: `/api/tcb/databasedelete`,
    method: 'post',
    data: {
      query: `db.collection('boxChapterContent')
      .where({
         _id:'${option}'
      }).remove()`
    }
  })
}